package com.yixing.tech.common.autoline.task;

import com.alibaba.fastjson.JSON;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.autoline.domain.AGVRasterRequest;
import com.yixing.tech.common.autoline.domain.StackerCraneWCSTaskRequest;
import com.yixing.tech.common.autoline.domain.WCSResponse;
import com.yixing.tech.common.utils.HttpClientUtil;
import com.yixing.tech.common.utils.HttpResult;
import com.yixing.tech.common.utils.SerializerUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.Asserts;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
public class WCSTaskUtil {

    public static void validateRequestParams(Object request) throws BusinessException {
        Asserts.check(request != null, "请求参数为空");
        Asserts.check(WCSConfig.getInstance() != null, "WCS配置为空");
        Asserts.check(WCSConfig.getInstance().getIp() != null, "WCS配置参数[ip]为空");
        Asserts.check(WCSConfig.getInstance().getPort() != null, "WCS配置参数[端口]为空");
    }

    public static HttpResult sendRequest(Object request, String uri) {
        String url = "http://" + WCSConfig.getInstance().getIp() + ":" + WCSConfig.getInstance().getPort() + uri;
        String jsonData = SerializerUtil.writeAsJSONString(request);
        log.info(jsonData);
        return HttpClientUtil.getInstance().postJson(url, jsonData);
    }

    /**
     * 请求生成WCS堆垛机任务
     * */
    public static WCSResponse genAgvTask(StackerCraneWCSTaskRequest request) throws BusinessException {
        WCSResponse response = new WCSResponse(1, "请求失败", null);

        validateRequestParams(request);

        if(ObjectUtils.isEmpty(request.getINSTRUCTION_CODE())) {    // 如果instruction_code为空，默认填充32位uuid
            request.setINSTRUCTION_CODE(UUID.randomUUID().toString().replace("-", ""));
        }

        List<StackerCraneWCSTaskRequest> requestList = new ArrayList<>();
        requestList.add(request);
        HttpResult result = sendRequest(requestList, URI.GEN_TASK);

        if(result.getStatus() != -1) {
            log.info(result.getBody());
            response = JSON.parseObject(result.getBody(), WCSResponse.class);
        }
        return response;
    }

    /**
     * 请求生成WCS堆垛机任务
     * */
    public static WCSResponse genAgvTask(List<StackerCraneWCSTaskRequest> taskRequestList) throws BusinessException {
        WCSResponse response = new WCSResponse(1, "请求失败", null);

        for(StackerCraneWCSTaskRequest request: taskRequestList) {
            validateRequestParams(request);

            if(ObjectUtils.isEmpty(request.getINSTRUCTION_CODE())) {    // 如果instruction_code为空，默认填充32位uuid
                request.setINSTRUCTION_CODE(UUID.randomUUID().toString().replace("-", ""));
            }
        }

        HttpResult result = sendRequest(taskRequestList, URI.GEN_TASK);
        log.info(result.getBody());

        if(result.getStatus() != -1) {
            log.info(result.getBody());
            response = JSON.parseObject(result.getBody(), WCSResponse.class);
        }
        return response;
    }

    /** AGV请求打开光栅 */
    public static WCSResponse openRaster(AGVRasterRequest request) throws BusinessException {
        WCSResponse response = new WCSResponse(1, "请求失败", null);

        validateRequestParams(request);

        HttpResult result = sendRequest(request, URI.AGV_REQUEST_OPEN);

        if(result.getStatus() != -1) {
            response = JSON.parseObject(result.getBody(), WCSResponse.class);
        }
        return response;
    }

    /** AGV请求关闭光栅 */
    public static WCSResponse closeRaster(AGVRasterRequest request) throws BusinessException {
        WCSResponse response = new WCSResponse(1, "请求失败", null);

        validateRequestParams(request);

        HttpResult result = sendRequest(request, URI.AGV_REQUEST_CLOSE);

        if(result.getStatus() != -1) {
            response = JSON.parseObject(result.getBody(), WCSResponse.class);
        }
        return response;
    }
}
